//
//  ZYNViewControllerForTapDetail.m
//  VideoPic
//
//  Created by dllo on 16/3/28.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ZYNViewControllerForTapDetail.h"
#import "zynAVPlayerSingleton.h"
#import "ZYNModelForSongList.h"
#import <YYWebImage.h>
#import "ZYNTableViewCellForListDetail.h"
#import "YMVideoPlaySlider.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
typedef NS_ENUM(NSInteger,ZYNButtonPlayType) {
    ButtonTypeForCycle,
    ButtonTypeForSingleCycle,
    ButtonTypeForOrder,
    ButtonTypeForRand
};
@interface ZYNViewControllerForTapDetail ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UIImageView *imageForSongPic;
@property (nonatomic, strong) UIButton *buttonForPlayOrPause;
@property (nonatomic, strong) UIButton *buttonForList;
@property (nonatomic, strong) UIImageView *imageViewForBack;
@property (nonatomic, strong) UIVisualEffectView *effectView;
@property (nonatomic, strong) UIButton *buttonForPackUp;
@property (nonatomic, strong) YMVideoPlaySlider *slider;
@property (nonatomic, strong) UIButton *buttonForPlayStyle;
@property (nonatomic, strong) UIButton *buttonForBackSong;
@property (nonatomic, strong) UIButton *buttonForNextSong;
@property (nonatomic, strong) UILabel *labelForLeft;
@property (nonatomic, strong) UILabel *labelForRight;
@property (nonatomic, strong) UIButton *buttonForSongsList;
@property (nonatomic, assign) CGFloat totalMusicDuration;
@property (nonatomic, retain) id observerName;
@property (nonatomic, strong) ZYNModelForSongList *modelSongs;
@property (nonatomic, strong) NSMutableArray *arrForSongList;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) NSDictionary *dic;
@property (nonatomic, strong) UILabel *labelForSongName;
@property (nonatomic, strong) UILabel *labelForSingerName;
@property (nonatomic, strong) NSMutableArray *arrForButtonImage;
@property (nonatomic, assign) NSInteger indexForArrForButtonImage;
@property (nonatomic, assign) NSInteger SongId;
@property (nonatomic, assign) ZYNButtonPlayType buttonType;
@property (nonatomic, strong) UIView *viewForSongsList;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation ZYNViewControllerForTapDetail

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePic) name:@"showPause" object:nil];
    [self handleData];
    [self createSubViews];
    [self addProgressObserver];
    [self addNotificationCenters];
    self.indexForArrForButtonImage = 0;

    self.arrForButtonImage = @[@"listRecycle", @"singleSongRecycle", @"shunxu", @"suiji"].mutableCopy;
}
- (void)handleData {
    
    self.modelSongs = self.model;
    self.arrForSongList = self.arr;
    NSLog(@"arrForSongList:%@", self.arrForSongList);
    self.index = self.xiaBiao;
    self.SongId = self.model.songId;
    
}
- (void)createSubViews {
    
    // 最下面的歌曲图片imageView
    self.imageViewForBack = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.imageViewForBack.userInteractionEnabled = YES;
    [self.view addSubview:self.imageViewForBack];
    self.imageViewForBack.yy_imageURL = [NSURL URLWithString:self.model.picUrl];
    
    // 背景图片的上的毛玻璃
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    self.effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    self.effectView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    [self.imageViewForBack addSubview:self.effectView];
    self.effectView.userInteractionEnabled = YES;

    // 毛玻璃上的歌曲图片
    self.imageForSongPic = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH)];
    self.imageForSongPic.yy_imageURL = [NSURL URLWithString:self.model.picUrl];
    [self.effectView addSubview:self.imageForSongPic];
    self.imageForSongPic.userInteractionEnabled = YES;
    
    // 退出当前页面button
    self.buttonForPackUp = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonForPackUp.frame = CGRectMake(WIDTH / 18, WIDTH  / 14, WIDTH / 12, WIDTH / 12);

    [self.imageForSongPic addSubview:self.buttonForPackUp];
    
    [self.buttonForPackUp setImage:[UIImage imageNamed:@"shouqi"] forState:UIControlStateNormal];
    [self.buttonForPackUp addTarget:self action:@selector(handlePackUp:) forControlEvents:UIControlEventTouchUpInside];
    
    // 毛玻璃上的滑动条
//    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(0, WIDTH, WIDTH, WIDTH / 14)];
//    [self.effectView addSubview:self.slider];
//    [self.slider addTarget:self action:@selector(handleSliderChange:) forControlEvents:UIControlEventValueChanged];
    self.slider = [[YMVideoPlaySlider alloc] initWithFrame:CGRectMake(0, WIDTH, WIDTH, WIDTH / 14)];
    [self.effectView addSubview:_slider];
    [self.slider addTarget:self action:@selector(handleSliderChange:) forControlEvents:UIControlEventValueChanged];
    self.slider.thumbImage = [UIImage imageNamed:@"slider"];
    
    
    // 左边的时间label
    self.labelForLeft = [[UILabel alloc] initWithFrame:CGRectMake(0, WIDTH + 20, WIDTH / 6, WIDTH / 10)];
    self.labelForLeft.text = @"00:00";
    self.labelForLeft.font = [UIFont systemFontOfSize:14];
    [self.effectView addSubview:self.labelForLeft];
    self.labelForLeft.textColor = [UIColor whiteColor];
    
    // 右边的时间label
    self.labelForRight = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - WIDTH / 6, WIDTH + 20, WIDTH / 6, WIDTH / 10)];
    self.labelForRight.text = @"00:00";
    self.labelForRight.textAlignment = NSTextAlignmentRight;
    [self.effectView addSubview:self.labelForRight];
    self.labelForRight.font = [UIFont systemFontOfSize:14];
    self.labelForRight.textColor = [UIColor whiteColor];
    
    // 歌曲名label
    self.labelForSongName = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 5 + 10, WIDTH + 20 + 10, WIDTH / 2 + 20, WIDTH / 12)];
    [self.effectView addSubview:self.labelForSongName];
    self.labelForSongName.text = self.model.name;
    self.labelForSongName.textAlignment = NSTextAlignmentCenter;
    self.labelForSongName.textColor = [UIColor whiteColor];
    
    self.labelForSingerName = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 5 + 20, WIDTH + 20 + 10 + WIDTH / 12, WIDTH / 2 , WIDTH / 12)];
    [self.effectView addSubview:self.labelForSingerName];
    self.labelForSingerName.text = self.model.singerName;
    self.labelForSingerName.textAlignment = NSTextAlignmentCenter;
    self.labelForSingerName.font = [UIFont systemFontOfSize:14];
    self.labelForSingerName.textColor = [UIColor whiteColor];
    
    // 歌曲播放方式的button
    self.buttonForPlayStyle = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonForPlayStyle.frame = CGRectMake(30, HEIGHT - 80, WIDTH / 12, WIDTH / 12);
//    self.buttonForStyle.backgroundColor = [UIColor yellowColor];
    [self.effectView addSubview:self.buttonForPlayStyle];
    [self.buttonForPlayStyle setImage:[UIImage imageNamed:@"listRecycle"] forState:UIControlStateNormal];
//    self.buttonForPlayStyle.imageView.image = [UIImage imageNamed:@"listRecycle"];
    [self.buttonForPlayStyle addTarget:self action:@selector(handlePlayStyle:) forControlEvents:UIControlEventTouchUpInside];
    
    // 上一曲button
    self.buttonForBackSong = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonForBackSong.frame = CGRectMake(30 + WIDTH / 12 + 20, HEIGHT - 80, WIDTH / 10, WIDTH / 10);
    [self.effectView addSubview:self.buttonForBackSong];
//    self.buttonForBackSong.backgroundColor = [UIColor greenColor];
    [self.buttonForBackSong setImage:[UIImage imageNamed:@"backSong"] forState:UIControlStateNormal];
    [self.buttonForBackSong addTarget:self action:@selector(handleBackSong:) forControlEvents:UIControlEventTouchUpInside];
    
    // 播放暂停button
    self.buttonForPlayOrPause = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonForPlayOrPause.frame = CGRectMake(30 + WIDTH / 12 + 20 + WIDTH / 10 + 30, HEIGHT - 90, WIDTH / 6, WIDTH / 6);
    [self.effectView addSubview:self.buttonForPlayOrPause];
    [self.buttonForPlayOrPause addTarget: self action:@selector(handlePlayOrPauseClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.buttonForPlayOrPause setImage:[UIImage imageNamed:@"playerr"] forState:UIControlStateNormal];
//    self.buttonForPlayOrPause.backgroundColor = [UIColor lightGrayColor];
    self.buttonForPlayOrPause.selected = YES;
    
    // 下一曲button
    self.buttonForNextSong = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonForNextSong.frame = CGRectMake(50 + WIDTH / 12 + 20 + WIDTH / 10 + 20 + WIDTH / 6 + 20, HEIGHT - 80, WIDTH / 10, WIDTH / 10);
//    self.buttonForNextSong.backgroundColor = [UIColor redColor];
    [self.effectView addSubview:self.buttonForNextSong];
    [self.buttonForNextSong setImage:[UIImage imageNamed:@"next"] forState:UIControlStateNormal];
    
    [self.buttonForNextSong addTarget:self action:@selector(handleNextSong:) forControlEvents:UIControlEventTouchUpInside];

    // 弹出歌曲列表button
    self.buttonForSongsList = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonForSongsList.frame = CGRectMake(50 + WIDTH / 12 + 20 + WIDTH / 10 + 20 + WIDTH / 6 + 20 + WIDTH / 10 + 20, HEIGHT - 75, WIDTH / 13, WIDTH / 13);
    [self.effectView addSubview:self.buttonForSongsList];
//    self.buttonForSongsList.backgroundColor = [UIColor yellowColor];
    [self.buttonForSongsList setImage:[UIImage imageNamed:@"list"] forState:UIControlStateNormal];
    self.buttonForSongsList.selected = YES;
    
    [self.buttonForSongsList addTarget:self action:@selector(handleShowList:) forControlEvents:UIControlEventTouchUpInside];
    self.viewForSongsList = [[UIView alloc] initWithFrame:CGRectMake(WIDTH / 4, WIDTH - 120, WIDTH / 3 * 2, WIDTH - 60)];
    [self.effectView addSubview:self.viewForSongsList];
//    self.viewForSongsList.backgroundColor = [UIColor lightGrayColor];
    self.viewForSongsList.hidden = YES;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.viewForSongsList.frame.size.width, self.viewForSongsList.frame.size.height) style:UITableViewStylePlain];
    [self.viewForSongsList addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[ZYNTableViewCellForListDetail class] forCellReuseIdentifier:@"pool1"];
    
}
- (void)handlePic {
    NSLog(@"dddd");
    [self.buttonForPlayOrPause setImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
}
// 自定义区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *viewForHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 3 * 2, 20)];
    viewForHeader.backgroundColor = [UIColor blackColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, WIDTH / 3 * 2 / 2, 15)];
    label.text = @"正在播放";
    label.font = [UIFont systemFontOfSize:15];
    [viewForHeader addSubview:label];
    label.textColor = [UIColor whiteColor];
    return viewForHeader;
}
// 区头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 30;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"111:%ld", self.arrForSongList.count);
    return self.arrForSongList.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYNTableViewCellForListDetail *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    ZYNModelForSongList *model = [self.arrForSongList objectAtIndex:indexPath.row];
    [cell passSongListModel:model];
    cell.labelForCurrentSongIndex.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    self.tableView.backgroundColor = [UIColor blackColor];
    cell.backgroundColor = [UIColor colorWithRed:15 / 256.f green:15 / 256.f blue:13 / 256.f alpha:1];

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYNModelForSongList *model = [self.arrForSongList objectAtIndex:indexPath.row];
    [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[model.urlList objectAtIndex:0] objectForKey:@"url"]];
    self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
    self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];
    self.labelForSongName.text = model.name;
    self.labelForSingerName.text = model.singerName;
    self.SongId = model.songId;// 记录歌曲的id,在单曲循环的时候用
    
}
- (void)handleSliderChange:(YMVideoPlaySlider *)slider {
    double currentTime = self.totalMusicDuration * slider.value;
    CMTime dragedCMTime = CMTimeMake(currentTime, 1);
    
    [[zynAVPlayerSingleton sharePlayer].player
     seekToTime:dragedCMTime completionHandler:^(BOOL finished) {
         
         [[zynAVPlayerSingleton sharePlayer].player play];
         
     }];
}
- (void)handleShowList:(UIButton *)button {
    if (button.selected) {
        button.selected = !button.selected;
        self.viewForSongsList.hidden = NO;
        
    } else {
        button.selected = !button.selected;
        self.viewForSongsList.hidden = YES;
        
    }
    
}

// 播放方式
- (void)handlePlayStyle:(UIButton *)button {
    
    if (_buttonType == ButtonTypeForRand) {
        _buttonType = ButtonTypeForCycle;
    }else _buttonType++;
    [self.buttonForPlayStyle setImage:[UIImage imageNamed:self.arrForButtonImage[_buttonType]] forState:UIControlStateNormal];
    NSLog(@"%ld", _buttonType);
    
    
    
    
    
    
//    if (self.indexForArrForButtonImage == 3) {
//        NSLog(@"%ld", self.indexForArrForButtonImage);
//        self.indexForArrForButtonImage = 0;
//        NSLog(@"hhhhhh:%ld", self.indexForArrForButtonImage);
//        [self.buttonForPlayStyle setImage:[UIImage imageNamed:[self.arrForButtonImage objectAtIndex:self.indexForArrForButtonImage]] forState:UIControlStateNormal];
//        // 随机
//        
//        
//    } else {
//        self.indexForArrForButtonImage += 1;
//        [self.buttonForPlayStyle setImage:[UIImage imageNamed:[self.arrForButtonImage objectAtIndex:self.indexForArrForButtonImage]] forState:UIControlStateNormal];
//        if (self.indexForArrForButtonImage == 0) {
//            NSLog(@"0");
//     
//        } else if (self.indexForArrForButtonImage == 1) {
//            NSLog(@"1");
//     
//        } else if (self.indexForArrForButtonImage == 2){
//            NSLog(@"2");
//          
//            
//        }
//    }
    
}
// 播放或暂停按钮的点击事件
- (void)handlePlayOrPauseClick:(UIButton *)button {
    
    if (button.selected) {
        button.selected = !button.selected;
        [self.buttonForPlayOrPause setImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
        [[zynAVPlayerSingleton sharePlayer].player pause];
        
    } else {
        
        button.selected = !button.selected;
        [self.buttonForPlayOrPause setImage:[UIImage imageNamed:@"playerr"] forState:UIControlStateNormal];
        [[zynAVPlayerSingleton sharePlayer].player play];
        
    }
    
}
// 下一曲button的点击事件
- (void)handleNextSong:(UIButton *)button {
    
    // 如果button的图片为随机的则下一曲的时候也是随机的
    if (self.buttonType == ButtonTypeForRand) {
          ZYNModelForSongList *model = self.arrForSongList[arc4random() % _arrForSongList.count];
        [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[model.urlList objectAtIndex:0] objectForKey:@"url"]];
        
        self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
        self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];
        self.labelForSongName.text = model.name;
        self.labelForSingerName.text = model.singerName;
        self.SongId = model.songId;
        NSLog(@"随机下一曲");
       
        // 如果button的图片不是随机的则下一曲的时候是正常顺序的
    } else {
        NSLog(@"正常顺序");
        if (self.index == [self.arrForSongList indexOfObject:[self.arrForSongList lastObject]]) {
            NSLog(@"last");
            self.index = 0;
            ZYNModelForSongList *model = [self.arrForSongList objectAtIndex:self.index];
            NSLog(@"111111:%@", model.urlList);
            [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[model.urlList objectAtIndex:0] objectForKey:@"url"]];
            self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
            self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];
            self.labelForSongName.text = model.name;
            self.labelForSingerName.text = model.singerName;
            self.SongId = model.songId;
            
            [self.delegate handleButtonClick:model];
            
        } else {
            self.index += 1;
            ZYNModelForSongList *model = [self.arrForSongList objectAtIndex:self.index];
            [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[model.urlList objectAtIndex:0] objectForKey:@"url"]];
            self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
            self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];
            self.labelForSongName.text = model.name;
            self.labelForSingerName.text = model.singerName;
            self.SongId = model.songId;
            [self.delegate handleButtonClick:model];
        }
        
    }
    
}
// 上一曲的点击事件
- (void)handleBackSong:(UIButton *)button {
    NSLog(@"index:%ld", self.index);
    if (self.buttonType == ButtonTypeForRand) {
        ZYNModelForSongList *model = self.arrForSongList[arc4random() % _arrForSongList.count];
        [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[model.urlList objectAtIndex:0] objectForKey:@"url"]];
        
        self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
        self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];
        self.labelForSongName.text = model.name;
        self.labelForSingerName.text = model.singerName;
        self.SongId = model.songId;
        NSLog(@"随机上一曲");
    
    } else {
        NSLog(@"正常顺序");
        if (self.index == 0) {
            NSLog(@"index2:%ld", [self.arrForSongList indexOfObject:[self.arrForSongList lastObject]]);
            self.index = [self.arrForSongList indexOfObject:[self.arrForSongList lastObject]];
            ZYNModelForSongList *model = [self.arrForSongList objectAtIndex:self.index];
            [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[model.urlList objectAtIndex:0] objectForKey:@"url"]];
            self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
            self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];
            self.labelForSongName.text = model.name;
            self.labelForSingerName.text = model.singerName;
            self.SongId = model.songId;
            
            [self.delegate handleButtonClick:model];
            
        } else {
            
            ZYNModelForSongList *model = [self.arrForSongList objectAtIndex:self.index -= 1];
            [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[model.urlList objectAtIndex:0] objectForKey:@"url"]];
            self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
            self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];
            self.labelForSongName.text = model.name;
            self.labelForSingerName.text = model.singerName;
            self.SongId = model.songId;
            
            [self.delegate handleButtonClick:model];
            
        }
    }
}
// 最上面的button的点击事件
- (void)handlePackUp:(UIButton *)button {
    [self dismissViewControllerAnimated:YES completion:nil];

}
#pragma 添加观察者
- (void)addProgressObserver {
    
    __weak zynAVPlayerSingleton *z = [zynAVPlayerSingleton sharePlayer];
    //设置(1,1) 设置每秒执行一次
    self.observerName =  [z.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        //总时长 将CMTime类型转成float类型
        CGFloat duration = CMTimeGetSeconds(z.playerItem.duration);
        // 当前时间
        CGFloat current = CMTimeGetSeconds(z.player.currentTime);
        
        // 倒计时
        //        CGFloat rem = duration - current;
        CGFloat rem = duration;
        
        NSString *totalT = [NSString stringWithFormat:@"%02d:%02d",(int)rem /60 ,(int)rem % 60];
        NSString *currenT = [NSString stringWithFormat:@"%02d:%02d",(int)current / 60 ,(int)current % 60];
        
        //        NSString *timeStr = [NSString stringWithFormat:@"%@ / %@",currenT,totalT];
        
        self.labelForLeft.text = currenT;
        self.labelForRight.text = totalT;
        // 保存总时长用于手动快进或快退
        self.totalMusicDuration = duration;
        
        // 设置进度值 当前时间/总时长
        
        self.slider.value = current / duration;
        
        NSArray *array = z.playerItem.loadedTimeRanges;
        CMTimeRange timeRange = [array.firstObject CMTimeRangeValue];
        float duration2 = CMTimeGetSeconds(z.playerItem.duration);
        float current2 = CMTimeGetSeconds(timeRange.duration);
        self.slider.bufferValue = current2 / duration2;
        
    }];
    
}
#pragma mark - 观察者观察播放是否完毕
- (void)addNotificationCenters {
    //  注册观察者用来观察，是否播放完毕
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];

}
#pragma mark 播放结束后
- (void)moviePlayDidEnd:(NSNotification *)notify {
    NSLog(@"ddd");
    switch (_buttonType) {
        case ButtonTypeForCycle:{
            NSLog(@"列表循环");
            if (self.index == [self.arrForSongList indexOfObject:[self.arrForSongList lastObject]]) {
                self.index = 0;
                ZYNModelForSongList *model = [self.arrForSongList objectAtIndex:self.index];
                [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[model.urlList objectAtIndex:0] objectForKey:@"url"]];
                self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
                self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];
                self.labelForSongName.text = model.name;
                self.labelForSingerName.text = model.singerName;
                self.SongId = model.songId;
                [self.delegate handleButtonClick:model];

                
            } else {
                NSLog(@"1236456:%@", self.arrForSongList);
                ZYNModelForSongList *model = [self.arrForSongList objectAtIndex:self.index += 1];
                [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[model.urlList objectAtIndex:0] objectForKey:@"url"]];
                self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
                self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];
                self.labelForSongName.text = model.name;
                self.labelForSingerName.text = model.singerName;
                self.SongId = model.songId;
                [self.delegate handleButtonClick:model];

            }
            
            break;
        }
        case ButtonTypeForSingleCycle:{
            NSLog(@"单曲循环");
            for (ZYNModelForSongList *model in self.arrForSongList) {
                if (model.songId == self.SongId) {
                    NSLog(@"12456789");
                    [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[model.urlList objectAtIndex:0] objectForKey:@"url"]];
                    self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
                    self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];
                    self.labelForSongName.text = model.name;
                    self.labelForSingerName.text = model.singerName;
                    
                    self.SongId = model.songId;
                    
                }
                
            }

            break;
        }
        case ButtonTypeForOrder:{
            NSLog(@"顺序播放");
            if (self.index == [self.arrForSongList indexOfObject:[self.arrForSongList lastObject]]) {
                NSLog(@"播放暂停");
            } else {
                ZYNModelForSongList *model = [self.arrForSongList objectAtIndex:self.index += 1];
                [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[model.urlList objectAtIndex:0] objectForKey:@"url"]];
                self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
                self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];
                self.labelForSongName.text = model.name;
                self.labelForSingerName.text = model.singerName;
                
            }

            break;
        }
        case ButtonTypeForRand:{
            NSLog(@"随机播放");
            NSLog(@"self.array:%@", self.arrForSongList);
            ZYNModelForSongList *model = self.arrForSongList[arc4random() % _arrForSongList.count];
            NSLog(@"name:%@", model.name);
            if (model.urlList.count == 0) {
                NSLog(@"123456");
                [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[model.mvList objectAtIndex:1] objectForKey:@"url"]];
            } else {
                [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[model.urlList objectAtIndex:1] objectForKey:@"url"]];
                NSLog(@"abcdefg");
            }

            self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
            self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];
            self.labelForSongName.text = model.name;
            self.labelForSingerName.text = model.singerName;
            self.SongId = model.songId;
            break;
        }
    }
    
    
    
    
    
//    if (self.indexForArrForButtonImage == 0) {
//        NSLog(@"列表循环");
//        if (self.index == [self.arrForSongList indexOfObject:[self.arrForSongList lastObject]]) {
//            NSLog(@"1111111");
//            self.index = 0;
//            ZYNModelForSongList *model = [self.arrForSongList objectAtIndex:self.index];
//            [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[model.urlList objectAtIndex:0] objectForKey:@"url"]];
//            self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
//            self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];
//            self.labelForSongName.text = model.name;
//            self.labelForSingerName.text = model.singerName;
//            self.SongId = model.songId;
//
//        } else {
//            ZYNModelForSongList *model = [self.arrForSongList objectAtIndex:self.index += 1];
//            [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[model.urlList objectAtIndex:0] objectForKey:@"url"]];
//            self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
//            self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];
//            self.labelForSongName.text = model.name;
//            self.labelForSingerName.text = model.singerName;
//            self.SongId = model.songId;
//
//        }
//        
//    } else if (self.indexForArrForButtonImage == 1) {
//        NSLog(@"单曲循环");
//        for (ZYNModelForSongList *model in self.arrForSongList) {
//            if (model.songId == self.SongId) {
//                NSLog(@"12456789");
//                [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[model.urlList objectAtIndex:0] objectForKey:@"url"]];
//                self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
//                self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];
//                self.labelForSongName.text = model.name;
//                self.labelForSingerName.text = model.singerName;
//                
//                self.SongId = model.songId;
//            
//            }
//            
//        }
//        
//    } else if (self.indexForArrForButtonImage == 2) {
//        NSLog(@"顺序播放");
//        if (self.index == [self.arrForSongList indexOfObject:[self.arrForSongList lastObject]]) {
//            NSLog(@"播放暂停");
//        } else {
//            ZYNModelForSongList *model = [self.arrForSongList objectAtIndex:self.index += 1];
//            [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[model.urlList objectAtIndex:0] objectForKey:@"url"]];
//            self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
//            self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];
//            self.labelForSongName.text = model.name;
//            self.labelForSingerName.text = model.singerName;
//        
//        }
//    } else if (self.indexForArrForButtonImage == 3) {
//        NSLog(@"随机播放");
//        
//        ZYNModelForSongList *model = self.arrForSongList[arc4random() % _arrForSongList.count];
//        NSLog(@"%@", model.name);
//        
//        
//    }
//    ZYNModelForSongList *model = [self.arrForSongList objectAtIndex:self.index += 1];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
